<template>
  <div class="authorization-section">
    <canvas class="canvas" id="pag"></canvas>
    <div class="header-bg">
      <span class="header-span">
        {{ titleName }}
      </span>
    </div>
    <div class="form-box">
      <div class="form-li">
        <p class="loginTitle">用户登录</p>
      </div>
      <div class="form-li">
        <img class="form-img" src="/img/yonghumin.png" alt="用户名" title="" />
        <el-input
          class="form-ipt"
          v-model="userName"
          placeholder="请输入用户名"
          clearable></el-input>
      </div>
      <div class="form-li">
        <img class="form-img" src="/img/password.png" title="密码" alt="" />
        <el-input
          class="form-ipt"
          type="password"
          v-model="userPassword"
          placeholder="请输入密码"
          clearable></el-input>
      </div>
      <div class="form-li">
        <el-button class="form-btn" :loading="loading" @click="login()">
          {{ '点击登录' }}
        </el-button>
      </div>
      <!-- <div class="form-li">
        <el-link type="primary" @click="bigImg()">点击扫描下载APP</el-link>
        <el-link type="primary" @click="againauthor()">重新授权</el-link>
      </div> -->
    </div>
    <el-dialog
      custom-class="bigimg-dialog"
      width="50%"
      v-model="dialogVisible"
      :modal-append-to-body="false"
      :before-close="toggleDialogCode"
      :modal="false"
      :fullscreen="true">
      <img src="/img/zddw.png" class="bigImg-box" />
      <span class="top-span" style="color: #aaadb4">{{ '浏览器扫描下载 麒云御空APP' }}</span>
    </el-dialog>
  </div>
</template>
<script setup>
  import { onMounted, ref, inject } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import { Login } from '@/api/login'
  import { useLoginStore } from '../stores/login'
  import { PAGInit } from 'libpag'
  const titleName = ref('低空防御指挥平台')
  const loginStore = useLoginStore()
  const router = useRouter()

  //弹出框
  const dialogVisible = ref(false)
  const toggleDialogCode = () => {
    dialogVisible.value = !dialogVisible.value
  }
  //判断是否授权
  const message = inject('$message')
  onMounted(() => {
    if (!!loginStore.state.token) {
      router.push('/')
    }
  })

  //登录
  const userName = ref('')
  const userPassword = ref('')
  const loading = ref(false)
  const login = () => {
    let params = {
      username: userName.value,
      password: userPassword.value,
    }
    if (userName.value && userPassword.value) {
      Login(params)
        .then((res) => {
          if (res.code == 200) {
            loading.value = false
            loginStore.setToken(res.data)
            loginStore.setAccount(userName.value)
            router.push('/')
          } else {
            loading.value = false
            message({
              message: res.msg,
              type: 'error',
            })
          }
        })
        .catch((err) => {
          loading.value = false
        })
    } else {
      message({
        message: '请输入用户名和密码',
        type: 'error',
      })
    }
  }
  PAGInit().then((PAG) => {
    //此处特别要注意一点，你得看你的文件路径是否能够获取到你想要的pag格式文件，如果不行就用../的形式
    //一层一层获取，目前在我的操作来看他目前貌似用不了 @/assets/XXX.pag的形式获取到想要的图片路径
    //大家可以自行尝试看能否可以获取到对应的pag图片路径
    const url = 'https://www.zjjnks.cn/glb/company-new-login-bg.pag'
    fetch(url)
      .then((response) => response.blob())
      .then(async (blob) => {
        const file = new window.File([blob], url.replace(/(.*\/)*([^.]+)/i, '$2'))
        const pagFile = await PAG.PAGFile.load(file)
        document.getElementById('pag').width = pagFile.width()
        document.getElementById('pag').height = pagFile.height()
        const pagView = await PAG.PAGView.init(pagFile, '#pag')
        pagView.setRepeatCount(0)
        await pagView.play()
      })
  })
</script>
<style lang="scss" scoped>
  .authorization-section {
    width: 100%;
    height: 100%;
    position: relative;
    &:before {
      content: '';
      width: 100vw;
      height: 100vh;
      background-size: 100% 100%;
      position: absolute;
      top: 0;
      left: 0;
    }
    .canvas {
      position: absolute;
      top: 0;
      left: 0;
    }
    .header-bg {
      position: absolute;
      width: 100%;
      height: 8vh;
      // background: url('/img/header.png') no-repeat;
      background-size: 100% 100%;
      z-index: 2;
      .header-span {
        margin-left: 1%;
        line-height: 60px;
        font-size: calc(100vw / 1920 * 38);
        font-family: Source Han Sans SC;
        font-weight: 400;
        color: #ffffff;
        text-shadow: 0px 2px 6px #007eff;
        background: linear-gradient(180deg, #a7d6ff 0%, #ffffff 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
    }
    .form-box {
      width: 22%;
      height: 46%;
      background-color: rgba(20, 45, 65, 0.6);
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      display: flex;
      flex-direction: column;
      flex-wrap: nowrap;
      align-items: center;
      justify-content: space-around;
      z-index: 5;
      border: 1px solid #4796e4;
      .loginTitle {
        width: 100%;
        font-size: 30px;
        font-family: Source Han Sans SC;
        color: #ffffff;
        text-align: center;
      }
      .form-li:first-child {
        margin-top: 1%;
      }
      .form-li {
        width: 80%;
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: center;
        justify-content: space-between;
        :deep(.el-input__inner) {
          background-color: transparent;
        }
        .form-img {
          width: 25px;
          height: 25px;
        }
        .form-label {
          width: 20%;
          text-align: right;
          font-size: 16px;
          color: #ffffff;
          letter-spacing: 5px;
        }
        .form-ipt {
          width: 100%;
          :deep(.el-input__inner) {
            color: #fff;
            border: none;
            border-radius: 0px;
            border-color: transparent;
            background-color: transparent;
            border-bottom: 1px solid rgba(71, 150, 228, 1);
          }
          :deep(.el-input__wrapper) {
            border-color: transparent;
            background-color: transparent;
            box-shadow: none;
          }
        }
        :deep(.el-input__inner::placeholder) {
          font-family: Source Han Sans CN;
          font-weight: 200;
          color: #cccccc;
          text-align: center;
        }
        .form-btn {
          margin: auto;
          width: 94%;
          height: 42px;
          color: #fff;
          background: #4796e4;
          border: 1px solid #4796e4;
          border-radius: 2px;
        }
      }
    }
    /* // reset 遮罩 */
    :deep(.bigimg-dialog) {
      background: rgba(8, 28, 59, 1);
      .el-dialog__header {
        padding-bottom: 40px;
      }
      .el-dialog__header .el-dialog__headerbtn .el-dialog__close {
        font-size: 30px;
        color: #909399;
      }
      .el-dialog__body {
        height: 80%;
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
      }
    }
  }
</style>
